package day52.org.controller;

import day52.basic.annotation.PreAuthorize;
import day52.basic.util.AjaxResult;
import day52.basic.util.PageList;
import day52.org.domain.Department;
import day52.org.query.DepartmentQuery;
import day52.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

//Restful风格
//@GetMapping      简单查询
//@PostMapping     高级查询
//@PutMapping      新增更新数据
//@DeleteMapping   删除数据
//@PatchMapping    批量操作


@RestController
@RequestMapping("/dept")
@Api(tags = "部门接口")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    @ApiOperation(value = "部门查找",notes = "根据部门id查找")
    @GetMapping("/{id}")
    public Department findOne(@PathVariable Long id){
        return departmentService.findOne(id);
    }

    @ApiOperation(value = "部门查找",notes = "查找所有部门")
    @GetMapping
    public List<Department> findAll(){
       return departmentService.findAll();
    }

    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")
    @PutMapping
    public AjaxResult save(@RequestBody @Valid Department department){
        if(department.getId() == null){
            departmentService.add(department);
        }else{
            departmentService.update(department);
        }
        return AjaxResult.me();
    }

    @ApiOperation(value = "部门删除",notes = "根据部门id进行删除")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        departmentService.delete(id);
        return AjaxResult.me();
    }
    @ApiOperation(value = "高级查询",notes = "进行分页等高级查询")
    @PostMapping
    @PreAuthorize(name = "部门列表",value = "department:list") //value后面用来做权限校验
    public PageList<Department> pageQuery(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.pageQuery(departmentQuery);
    }

    @ApiOperation(value = "批量删除",notes = "根据部门id进行删除")
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        departmentService.patchDelete(ids);
        return AjaxResult.me();
    }

    @GetMapping("/tree")
    public List<Department> queryTree(){
        return departmentService.queryTree();
    }
}
